package com.xizimall.sso.controller;

import java.lang.ProcessBuilder.Redirect;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
/**
 * 用户校验Controller
 * <p>Title: UserController</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月26日下午4:30:27
 * @version 1.0
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xizimall.pojo.TbUser;
import com.xizimall.sso.service.UserService;
import com.xizimall.sso.service.impl.UserServiceImpl;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.CookieUtils;
import com.xizimall.common.utils.ExceptionUtil;
@Controller
@RequestMapping("/user")
public class UserController {
    
	@Autowired
	private UserService userService;
	
	@RequestMapping("/check/{param}/{type}")
	@ResponseBody
	public Object checkData(@PathVariable String param,@PathVariable Integer type,String callback){
		XizimallResult result=null;
		//参数有效性校验
		/*
		 * jsp中已经实现参数不能为空
		 * if(StringUtils.isBlank(param)){
			result= xizimallResult.build(400, "校验内容不能为空");
		}
		if(type==null){
			result= xizimallResult.build(400, "校验类型不能为空");
		}*/
		if(type!=1&&type!=2&&type!=3){
			 result=XizimallResult.build(400, "校验类型错误");
		}
		//校验发现错误
		if(null!=result){
			if(null!=callback){
				MappingJacksonValue mappingJacksonValue=new MappingJacksonValue(result);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			}else{
				return result;
			}
		}
		//调用服务
		try {
			result=userService.checkData(param, type);
		} catch (Exception e) {
			result=XizimallResult.build(400, ExceptionUtil.getStackTrace(e));
		}
		if(null!=callback){
			MappingJacksonValue mappingJacksonValue=new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}else{
			return result;
		}
	}
	@RequestMapping("/register")
	@ResponseBody
	public XizimallResult createUser(TbUser user){
		try {
			XizimallResult result = userService.createUser(user);
			return result;
		} catch (Exception e) {
			return XizimallResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
	//用户登录
	@RequestMapping(value="/login",method = RequestMethod.POST)
	@ResponseBody
	public XizimallResult userLogin(String username,String password,HttpServletRequest request
			,HttpServletResponse response){
		try {
			XizimallResult result=userService.userLogin(username, password,request,response);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return XizimallResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
	@RequestMapping("/token/{token}")
	@ResponseBody
	public Object getUserByToken(@PathVariable String token,String callback){
		XizimallResult result = null;
		try {
			result = userService.getUserByToken(token);
			
		} catch (Exception e) {
			e.printStackTrace();
			return XizimallResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		//判断是否为jsonp调用
		if(StringUtils.isBlank(callback)){
			return result;
		}else{
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}
	}
	@RequestMapping("/logout")
	public void userLogout(HttpServletRequest request, HttpServletResponse response){
		userService.userLogout(request,response);
	}
}
